﻿using AutoMapper;
using Management.Infrastructure.Interfaces.BOMManagement;
using Management.Errorcode.ErrorCode;
using Management.API.Application.Controller.Management.Material;
using Management.API.DTOs.Management;
using Management.Infrastructure.Repositories.BOMManagement;
using MediatR;


namespace Management.API.Application.CommandHandler.Management.Material
{
    public class GetMaterialTypeCommandHandler: IRequestHandler<GetMaterialTypeCommand,ApiResult<IList<GetMaterialTypeDTO>>>
    {
        private readonly IMaterialTypeRepository iMaterialTypeRepository;
        private readonly IMapper _mapper;
        private readonly ILogger<GetMaterialTypeCommandHandler> _logger;

        public GetMaterialTypeCommandHandler(  IMaterialTypeRepository IMaterialTypeRepository, IMapper mapper,
            ILogger<GetMaterialTypeCommandHandler> logger)
        {
            iMaterialTypeRepository = IMaterialTypeRepository;
            _mapper = mapper;
            _logger = logger;
        }


        public async Task<ApiResult<IList<GetMaterialTypeDTO>>> Handle(GetMaterialTypeCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var useList = await iMaterialTypeRepository.GetAllAsync();
                var useLists = _mapper.Map<IList<GetMaterialTypeDTO>>(useList);
                return ApiResult<IList<GetMaterialTypeDTO>>.Success(useLists, StatusCodeEnum.Success);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "GetMaterialTypeCommandHandler");
                throw;
            }

        }





    }
}
